﻿using System;

namespace DotNetImage.Imaging.Filters
{
    public class BrightnessFilter : FilterBase
    {
        private uint BrightnessShift = 0;

        /// <summary>
        /// Brightness in percents. Can be from -100% (black) to +100% (white)
        /// </summary>
        [ParameterAttribute(Name = "Brightness", DefaultValue = 0, MinValue = -100, MaxValue = 100, Step = 0.1)]
        public long Brightness { get; set; }


        protected override void ValidateArguments()
        {
            Validator.ValidateInclusiveRange("Brightness", Brightness, -100, 100);
        }

        protected override void PrepareData(DotNetImage bmp)
        {
            base.PrepareData(bmp);
            BrightnessShift = (uint)(Brightness * bmp.ColorChannelMaxValue / 100);
        }


        protected override void ProcessPixel(ref Pixel pix, uint x, uint y)
        {
            Pixel brightness = pix.Clone();
            if (BrightnessShift >= 0)
            {
                brightness.Red = brightness.Green = brightness.Blue = BrightnessShift;
                pix.Add(brightness);
            }
            else
            {
                brightness.Red = brightness.Green = brightness.Blue = (uint)Math.Abs(BrightnessShift);
                pix.Subtract(brightness);
            }
        }
    }
}
